package com.andnnl.bitmap;

import cn.hutool.bloomfilter.bitMap.BitMap;
import cn.hutool.bloomfilter.bitMap.LongMap;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by chenss on 2021/4/26.
 */
public class MobileBitMapTest {
    //        10亿数据除64=15625000
    static int max = 2000000000;//20亿
    static int size = max / BitMap.MACHINE64;
    static long maxMobile=10000000000L;//100亿
    static BitMap bm1 = new LongMap(size);
    static BitMap bm2 = new LongMap(size);
    static BitMap bm3 = new LongMap(size);
    static BitMap bm4 = new LongMap(size);
    static BitMap bm5 = new LongMap(size);
    static List<BitMap> mbList = Arrays.asList(bm1, bm2, bm3, bm4, bm5);

    public static void main(String[] args) throws IOException {

        System.out.println(size);
//        test1();
        testRand();


        System.in.read();
    }

    private static void testRand() {
        ExecutorService pool = Executors.newFixedThreadPool(100);
        int size = 10000 * 100;
        long[] list=new long[size];
        for (int i = 0; i < size; i++) {
            long v = RandomUtil.randomLong(maxMobile / 10, maxMobile);
            list[i]=v;
        }
        System.out.println("gen finish...");
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            pool.execute(()->{
                testRandMobile2(list);
                System.out.println("finish..."+ finalI);
            });
        }
    }

    private static void testRandMobile2(long[] list) {
        for (int i = 0; i < list.length; i++) {
            long v=list[i];
            boolean rs=addAndGet(v);
            if(!rs){
                System.out.println(v);
            }
        }
    }

    private static void testRandMobile() {
        for (int i = 0; i < 10000*100; i++) {
            long v=RandomUtil.randomLong(maxMobile/10,maxMobile);
            boolean rs=addAndGet(v);
            if(!rs){
                System.out.println(v);
            }
        }
    }

    private static void test1() {
        for (long i = 0; i < maxMobile; i=i+1) {
            boolean rs=addAndGet(i);
            if(rs){
//                System.out.println(i);
            }
        }
    }

    private static boolean addAndGet(long i) {
        int idx= (int) (i/max);
        BitMap bm = mbList.get(idx);
        long v=i%max;
        if(v%100000000==0){
            System.out.println(v+" "+i+" "+idx);
        }
        bm.add(v);
        return bm.contains(v);
    }
}
